package paper.web.controller.topic;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import keter.domain.validation.BeanValidators;
import keter.web.util.MediaTypes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import paper.dao.CommentsDao;
import paper.domain.Comments;
import paper.domain.Topic;
import paper.domain.UploadItem;
import paper.service.TopicService;
import paper.service.UserService;
import paper.web.controller.BaseController;

/**
 * <p>Class       : paper.web.controller.LoginController
 * <p>Descdription: 登录跳转逻辑控制器
 * @author  顾力行-gulixing@msn.com
 * @version 1.0.0
 */  
@Controller
@RequestMapping("/paper/topic/student_topic")
public class StudentTopicController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(StudentTopicController.class);

    @Autowired
    TopicService service;
    
    @Autowired
    UserService us;
    
	@Autowired
	CommentsDao cdao;
    
	public static class Views {
		private static final String Prefix = "paper/topic/student_topic";
		static final String Index = to_(Prefix) + "index";
		static final String Update = to_(Prefix) + "update";
	}
	
	public StudentTopicController() {
		
	}
	
	/**
	 * <p>Method ：getCurrentReport
	 * <p>Description : 学生获取当前唯一报告
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	private Topic getCurrentTopic(){
		List<Topic> list = service.getTopicDao().findByCreator(getCurrentUser().getId());
		if(list!=null && !list.isEmpty()){
			return list.get(0);
		}
		return new Topic();
	}
	
	/**
	 * <p>Method ：index
	 * <p>Description : 进入页面：直接展示报告
	 * @param model
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.Index, method = RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute("topic", getCurrentTopic());
		if(getCurrentTopic().getId()!=null){
			model.addAttribute("creatorName", us.getDao().findOne(getCurrentTopic().getCreatorId()).getName());
		}
		List<Comments> list = cdao.findByMainId(getCurrentTopic().getId());
		model.addAttribute("comments", list);
		model.addAttribute("count", list.size());
		return Views.Index;
	}
	
	/**
	 * <p>Method ：update
	 * <p>Description : 进入创建|修改页面
	 * @param model
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(Model model) {
		model.addAttribute(new UploadItem());
		model.addAttribute("topic", getCurrentTopic());
		return Views.Update;
	}
	
	/**
	 * <p>Method ：update
	 * <p>Description : 更新报告
	 * @param uploadItem
	 * @param model
	 * @param request
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Model model, HttpServletRequest request) {
		MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
		// 1. 新建或更新报告
		Topic report = getCurrentTopic();
		report.setCreatorId(getCurrentUser().getId());
		report.setFinisher(getCurrentUser());
		report.setName(req.getParameter("name"));
		report.setContent(request.getParameter("content"));
		BeanValidators.validateWithException(validator, report);
		service.getTopicDao().save(report);
		String redirectUrl = "/paper/topic/student_topic/update";
        model.addAttribute("updateMessage", "保存成功！");
        return redirectToUrl(redirectUrl);
    }
	
	/**
	 * <p>Method ：submit
	 * <p>Description : 学生提交报告
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = "/submit", method = RequestMethod.POST, consumes = MediaTypes.JSON)
	@ResponseBody
	public Map<String, String> submit() {
		Map<String, String> ajaxMap = getAjaxMap();
		Topic t = getCurrentTopic();
		t.setMaster(getCurrentUser().getTeacher());
		service.submit(t);
		return ajaxMap;
	}
	
	/**
	 * <p>Method ：comments
	 * <p>Description : 评论查看
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = "/viewcomments", method = RequestMethod.GET)
	@ResponseBody
	public List<Comments> comments() throws UnsupportedEncodingException {
    	return cdao.findByMainId(getCurrentTopic().getId());
	}
}